#include "contigdatapacket.h"

//TO DO:  delete strategy needs to be figured out

ContigDataPacket::ContigDataPacket(QString contigId, bool hasSpec, QString spec, QObject *parent) :
    contigId(contigId), QObject(parent)
{
    hasSpecies = hasSpec;
    species = spec;
    length = 0;
}

ContigDataPacket::~ContigDataPacket()
{
    //I removed this because when I add contigDataPacket to database, I delete it before all the genome elements are created
//    while (!genomeElementDataPackets.isEmpty()) //empty cache
//        if( ! genomeElementDataPackets.takeFirst() )
//         delete genomeElementDataPackets.takeFirst();

//    while( !genomeElementDataPackets.isEmpty() )
//    {
//        if( genomeElementDataPackets.takeFirst() );
//            delete genomeElementDataPackets.takeFirst();
//    }

    qDebug() << "deleting contig data packet" << endl;
    while( ! genomeElementDataPackets.isEmpty() )
    {
        GenomeElementDataPacket* g = genomeElementDataPackets.takeFirst();
        if( g != 0 )
        {
            delete g;
            g = 0;
        }
    }
}

QString ContigDataPacket::getContigId()
{
   return contigId;
}

QString ContigDataPacket::getSpecies()
{
    return species;
}

bool ContigDataPacket::getHasSpecies()
{
    return hasSpecies;
}

void ContigDataPacket::addGenomeElement(QPointer<GenomeElementDataPacket> p)
{
    genomeElementDataPackets << p ;

    if( p->getStop() > length )
        length = p->getStop(); //TO DO:  this isn't really the length- just the last listed elt.
}

int ContigDataPacket::numOfGenomeElements()
{
    return genomeElementDataPackets.size();
}

GenomeElementDataPacket* ContigDataPacket::getGenomeElementDataPacket( int i )
{
    return genomeElementDataPackets[i];
}

int ContigDataPacket::getLength()
{
    return length;
}

QString ContigDataPacket::printOutContigData()
{
    QString toPrint = "";
    toPrint.append("Contig:   ").append("\t").append( contigId ).append("\t")
            .append("length:").append("\t").append(QString::number(length)).append("\t")
            .append("hasSpecies").append("\t").append(QString(hasSpecies?"T":"F")).append("\t")
            .append("species").append("\t").append(species).append('\n');
    toPrint.append("begin genes:\n");
    for(int i = 0; i < genomeElementDataPackets.size(); i++)
    {
        toPrint.append(genomeElementDataPackets[i]->toString() ).append("\n");
    }
    toPrint.append("end genes:\n");

    return toPrint;
}
